import java.io.IOException;
import java.util.Arrays;

/**
 * Created by blatand on 2017/2/9.
 */
public class MsgUnPack {
    public static byte[] ParseBitMap(byte [] b,int domainNum){

        int temp = 0;

        byte[] bt4 = new byte[128];
        int i,j = 0;
        System.out.printf("ParseBitMap domainNum = %d\r\n",domainNum);
//        if(domainNum!=8||domainNum!=16)return null;

        for(j = 0;j<domainNum;j++){

            temp = b[j];
            for(i = 0;i<8;i++){
                if((temp&0x80)==0x80){
                    bt4[i+j*8] = '1';
//                    System.out.printf("1 ");
                }else{
                    bt4[i+j*8] = '0';
//                    System.out.printf("0 ");
                }
                temp = temp << 1;
            }
        }
        return bt4;
    }
    public  static Msg8583Util ParseMessage(byte[] msg, String filePath){

        int i,msgPos=0;
        int len=0;
        int maxDomainNum = 0;
        byte[][] msgDat = new byte[129][1024];
        Msg8583Util  msgParseApp = new Msg8583Util();
        byte[] bt64 = new byte[128];
        byte[] bitmap = new byte[16];

        System.out.printf("%d ",msg[0]);
        System.out.printf("%d ",0x80);
        if(msg[0]>0x80){

            maxDomainNum = 128;
            System.arraycopy(msg,0,bitmap,0,16);
        }else{
            maxDomainNum = 64;
            System.arraycopy(msg,0,bitmap,0,8);
        }
//        if(msg[0] - 0x80 <= 0){
//            maxDomainNum = 128;
//            System.arraycopy(msg,0,bitmap,0,16);
//        }else{
//            maxDomainNum = 64;
//            System.arraycopy(msg,0,bitmap,0,8);
//        }
        msgPos = maxDomainNum/8;
        bt64 = ParseBitMap(bitmap,msgPos);


        for(i=0;i<msg.length;i++){
            System.out.printf("%02x ",msg[i]);
        }

        for(i=2;i<=maxDomainNum;i++)
        {
            if(bt64[i-1]=='1'){
                len = getBitMapType(i,filePath);
                System.out.printf("Field%02d Len= %02d \r\n",i,len);
                switch(len) {
                    case Msg8583Util.TYPE_LLVAR:
                        len = ToolUtil.bytesToInt(msg[msgPos]);
                        msgPos += 1;
                        break;
                    case Msg8583Util.TYPE_BCDLLVAR:
                        len = (ToolUtil.bytesToInt(msg[msgPos]) + 1)/2;
                        msgPos += 1;
                        break;
                    case Msg8583Util.TYPE_LLLVAR:
                        len = ToolUtil.bytesToInt(msg[msgPos]) * 100 + ToolUtil.bytesToInt(msg[msgPos+1]);
                        msgPos += 2;
                        break;
                    case Msg8583Util.TYPE_BCDLLLVAR:
                        len = ToolUtil.bytesToInt(msg[msgPos]) * 100 + ToolUtil.bytesToInt(msg[msgPos+1]);
                        len = (len+1) /2;
                        msgPos += 2;
                        break;
                    default:
                        break;
                }
                System.arraycopy(msg,msgPos,msgDat[i],0,len);
                msgPos += len;
                msgParseApp.setBitValue(i,len,msgDat[i]);
                ToolUtil.TraceHexString(i,msgDat[i],len);
            }
        }
        return msgParseApp;
    }
    public MsgUnPack() {

//        bmAnalysz = new msg8583();

    }

    private static int parseBitMapType(int domain) {

        int type = 0;

        switch(domain){

            case 3:
            case 11:
            case 12:
            case 49:
                type = 3;
                break;
            case 4:
            case 5:
                type = 6;
                break;
            case 13:
            case 14:
            case 22:
            case 23:
                type = 2;
                break;
            case 25:
            case 55:
                type = 1;
                break;
            case 37:
                type = 12;
            case 42:
                type = 15;
                break;
            case 41:
            case 52:
            case 64:
                type = 8;
                break;


            case 2:
            case 35:
            case 36:
                type = Msg8583Util.TYPE_LLVAR;
                break;
            case 7:
            case 32:

                type = Msg8583Util.TYPE_BCDLLVAR;
                break;

            case 48:
            case 57:
            case 59:
            case 60:
            case 61:
            case 62:
            case 63:
                type = Msg8583Util.TYPE_LLLVAR;
                break;

        }
        return type;
    }
    static int getBitMapType(int i,String bmFname) {

        int typeLen ;
        String value = null;
        String section_name ;
        if(bmFname.equals(null)) {
            System.out.printf("bmFname is empty");
            return 0;
        }
        if(i<0||i>128){
            return 0;
        }

        ProfileString ConfigurationFile = new ProfileString();

        section_name = String.format("Field%03d",i);
//        System.out.printf("section_name = %s  ",section_name);
        try {
            value = ConfigurationFile.getProfileString(bmFname , "FieldType" , section_name , "default");

        } catch (IOException e1) {

            e1.printStackTrace();
        }

        if(value.equals("default")){
            typeLen = 0;
        }
        else{
            typeLen = Integer.parseInt(value);
        }
//        System.out.printf("typeLen = %d\r\n",typeLen);
        return typeLen;
    }
}
